package com.daiqile.mysupertab;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.daiqile.mysupertab.base.BaseActivity;
import com.daiqile.mysupertab.fragment.FourFragment;
import com.daiqile.mysupertab.fragment.OneFragment;
import com.daiqile.mysupertab.fragment.ThreeFragment;
import com.daiqile.mysupertab.fragment.TwoFragment;
import com.daiqile.mysupertab.view.ViewPagerFix;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity {


    @BindView(R.id.viewPage)
    ViewPagerFix viewPage;
    @BindView(R.id.bottomNB)
    BottomNavigationBar bottomNB;

    private Activity mActivity;
    private ViewPagerAdapter adapter;
    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    private ThreeFragment threeFragment;
    private FourFragment fourFragment;
    private List<Fragment> fragment = new ArrayList<>();
    @Override
    public void init() {
        mActivity = MainActivity.this;
        oneFragment = new OneFragment();
        twoFragment = new TwoFragment();
        threeFragment = new ThreeFragment();
        fourFragment = new FourFragment();
        fragment.add(oneFragment);
        fragment.add(twoFragment);
        fragment.add(threeFragment);
        fragment.add(fourFragment);
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        //set Viewpage

        viewPage.setAdapter(adapter);
        viewPage.setScrollable(false);
        viewPage.setOffscreenPageLimit(fragment.size());
        //set BottomNavigationBar
        bottomNB.setMode(BottomNavigationBar.MODE_SHIFTING)
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE)
                .setActiveColor(R.color.material_white)//在BACKGROUND_STYLE_STATIC 模式下颜色是图标和文字的颜色（选中/未选中），在BACKGROUND_STYLE_RIPPLE 模式下是底部导航栏背景色。
                .setInActiveColor(R.color.material_black)
                .setBarBackgroundColor(R.color.colorWhite);//在BACKGROUND_STYLE_STATIC 模式下颜色是底部导航栏背景色，BACKGROUND_STYLE_RIPPLE模式下是图标和文字的颜色（选中/未选中）
        BottomNavigationItem item1 = new BottomNavigationItem(R.mipmap.icon_index_touch, getString(R.string.mxmain)).setInactiveIconResource(R.mipmap.icon_index).setActiveColorResource(R.color.material_orange_300);
        BottomNavigationItem item2 = new BottomNavigationItem(R.mipmap.icon_index_touch, getString(R.string.mxshop)).setInactiveIconResource(R.mipmap.icon_index).setActiveColorResource(R.color.material_blue_200);
        BottomNavigationItem item3 = new BottomNavigationItem(R.mipmap.icon_index_touch, getString(R.string.mxmore)).setInactiveIconResource(R.mipmap.icon_index).setActiveColorResource(R.color.material_amber_200);
        BottomNavigationItem item4 = new BottomNavigationItem(R.mipmap.icon_index_touch, getString(R.string.mxcenter)).setInactiveIconResource(R.mipmap.icon_index).setActiveColorResource(R.color.material_deepOrange_200);
        bottomNB.addItem(item1)
                .addItem(item2)
                .addItem(item3)
                .addItem(item4)
                .setFirstSelectedPosition(0)
                .initialise();
        bottomNB.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                viewPage.setCurrentItem(position);

            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });



    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public Activity bindActivity() {
        return this;
    }


    private class ViewPagerAdapter extends FragmentPagerAdapter{

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragment.get(position);
        }

        @Override
        public int getCount() {
            return fragment.size();
        }
    }
}
